package com.raibaz.atm;

import java.util.List;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;

public class AtmLocationListener implements LocationListener {
	
	Location currentLocation = null;
	String currentFrom = "";
	Atm atm;

	public AtmLocationListener(Atm atm) {
		this.atm = atm;
	}
	
	
	public void onLocationChanged(Location location) {

		Geocoder gc = new Geocoder(atm.getBaseContext());
		try {
			List<Address> addresses = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
			String ret = "";
			if(!addresses.isEmpty()) {
				Address add = addresses.get(0);
				for(int i = 0; i < add.getMaxAddressLineIndex(); i++) {
					ret += add.getAddressLine(i);
				}				
			} else {
				Toast.makeText(atm.getBaseContext(), "Location not found", Toast.LENGTH_LONG);
			}
			atm.gpsLocationReceived(ret);
			
		} catch (Exception e) {
			Toast.makeText(atm.getBaseContext(), "Error geocoding location", Toast.LENGTH_LONG);
		}
	}

	
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
